<template>
  <!-- 试乘试驾申请 -->
  <div>
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhapply"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :isSelectColumn="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addFn()">新增</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="isAdd" destroyOnClose :title="'试乘试驾车新增'">
      <addModel :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>

    <BaseDialog :visible.sync="isEdit" destroyOnClose :title="'试乘试驾车编辑'">
      <editModel :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>

    <Drawer :visible.sync="isView" :title="'试乘试驾车明细'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <viewModel :rowData="formData" @close="emitCloseHandler" />
    </Drawer>

    <Drawer :visible.sync="isOrder" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <orderModel :rowData="formData" @close="closeOrderFn" @save="saveOrderFn" />
    </Drawer>
  </div>
</template>
<script>
import { vehicleConfiguration } from '@/api/omFinance';
import { queryList, saveData, reviewData } from '@/api/dealerSale/TestDrive/apply';
import { dictionary } from '@/utils/dictMixin';
import addModel from './addModel.vue';
import editModel from './editModel.vue';
import orderModel from './orderModel.vue';
import viewModel from './viewModel.vue';

export default {
  name: 'apply',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: { queryType: 'apply', isDealer: 1 },
      formData: {},
      isAdd: false,
      isEdit: false,
      isView: false,

      isLock: false,
      lockDisable: 0,
      selectList: [],
      isOrder: false
    };
  },
  mixins: [dictionary],
  components: {
    addModel,
    orderModel,
    editModel,
    viewModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList(); // 加载翻译字典
  },
  methods: {
    viewFn(row) {
      this.formData = row;
      this.isView = true;
    },
    addFn() {
      this.isAdd = true;
    },
    emitCloseHandler(val, type) {
      this.isAdd = val;
      this.isEdit = val;
      this.isView = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async cancelHandle(row, status) {
      let params = {
        id: row.id,
        queryType: status,
        checkStatus: 60761007
      };
      const res = await reviewData(params);

      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '取消成功' });
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    async submitFn(row) {
      let params = {
        id: row.id,
        checkStatus: 60761002
      };
      const res = await saveData(params);

      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '提交成功' });
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    getList() {
      this.getvehicleList();
    },
    showOrderFn(data) {
      this.formData = data;
      this.isOrder = true;
    },
    closeOrderFn() {
      this.isOrder = false;
    },
    saveOrderFn() {
      this.isOrder = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    editHandle(row) {
      this.formData = row;
      this.isEdit = true;
    },
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE', // 别忘记处理查询一天查询不到数据问题
          label: '录入日期',
          fieldName: 'beginCreatedAt|endCreatedAt',
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'brandProductList',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装'] },
          itemList: []
        },
        // {
        //   type: 'SELECT',
        //   label: '是否首台',
        //   fieldName: 'isFirst',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004')
        // }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 180,
          fixed: 'left',
          align: 'center',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" disabled={props.row.checkStatus != 60761001 && props.row.checkStatus != 60761007} onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.viewFn(props.row)}>
                  明细
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" disabled={props.row.checkStatus != 60761002} onClick={() => this.cancelHandle(props.row, 'delete')}>
                  取消
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" disabled={props.row.checkStatus != 60761001 && props.row.checkStatus != 60761007} onClick={() => this.submitFn(props.row)}>
                  提交
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '申请状态',
          dataIndex: 'checkStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6076'),
          render: props => {
            return <span>{this.createDictText(props.row.checkStatus, '6076')}</span>;
          }
        },
        {
          title: '申请单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (props, h) => {
            return (
              <multiuse-button size="mini" type="text" onClick={() => this.showOrderFn(props.row)}>
                {props.row.orderNo}
              </multiuse-button>
            );
          }
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '在途车', value: 60401002 },
            { text: '库存车', value: 60401003 }
          ],
          render: props => {
            return (
              <span>
                {this.transDictText(props.row.orderType, [
                  { codeCnDesc: '在途车', codeId: 60401002 },
                  { codeCnDesc: '库存车', codeId: 60401003 }
                ])}
              </span>
            );
          }
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报数量',
          dataIndex: 'reportCount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '录入日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        // {
        //   title: '是否首台',
        //   dataIndex: 'isFirst',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('1004'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.isFirst, '1004')}</span>;
        //   }
        // },
        {
          title: '折扣后单价',
          dataIndex: 'unitPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
